<template>
  <!-- SPU管理 -->
  <div>
    <Category :scene="scene" />
    <el-card style="margin: 15px 0px">
      <!-- 展示SPU -->
      <div v-show="scene == 0">
        <el-button @click="addSpu" type="primary" icon="Plus" :disabled="!categoryStore.c3Id">添加SPU</el-button>
        <!-- SPU列表 -->
        <el-table border style="margin: 15px 0px" :data="records">
          <el-table-column label="序号" type="index" align="center" width="100px"></el-table-column>
          <el-table-column label="SPU名称" align="center" width="200px" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作" align="center">
            <template #default="{ row }">
              <el-button type="primary" icon="Plus" title="添加SKU" @click="addSku(row)"></el-button>
              <el-button type="warning" icon="Edit" title="修改SPU" @click="updateSpu(row)"></el-button>
              <el-button type="info" icon="infoFilled" title="查看SKU列表" @click="findSku(row)"></el-button>
              <el-popconfirm :title="`你确定要删除${row.spuName}吗`" width="200px" @confirm="deleteSpu(row)">
                <template #reference>
                  <el-button type="danger" icon="Delete" title="删除SPU"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]"
          :background="true" layout="jumper, prev, pager, next, ->, total, sizes" @current-change="getHasSpu"
          @size-change="getHasSpu(1)" :total="total" />
      </div>
      <!-- 添加或修改SPU -->
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spuForm" />
      <!-- 添加SKU -->
      <SkuForm v-show="scene == 2" @changeScene="changeScene" ref="skuForm" />
      <!-- 展示SKU -->
      <el-dialog title="SKU列表" v-model="show">
        <el-table :data="skuArr">
          <el-table-column prop="skuName" label="sku名字">
          </el-table-column>
          <el-table-column prop="price" label="sku价格">
          </el-table-column>
          <el-table-column prop="weight" label="sku重量">
          </el-table-column>
          <el-table-column label="sku图片">
            <template #="{ row, $index }">
              <img :src="row.skuDefaultImg" alt="" width="100px">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import Category from "@/components/Category/index.vue";
import { onBeforeUnmount, ref, watch } from "vue";
import useCategoryStore from "@/store/modules/category";
import { reqHasSpu, reqRemoveSpu, reqSkuList } from "@/api/product/spu";
import { HasSpuResponseData, Records, skuData, skuInfoData, SpuData } from "@/api/product/spu/type";
import SpuForm from "./spuForm.vue";
import SkuForm from "./skuForm.vue";
import { ElMessage } from "element-plus";

//#region 数据定义
let categoryStore = useCategoryStore();
// 场景
let scene = ref<number>(0);
// 分页器相关数据
let pageNo = ref<number>(1);
let pageSize = ref<number>(3);
let total = ref<number>(0);
// SPU数据
let records = ref<Records>([]);
// 添加SPU组件
let spuForm = ref<any>()
// 添加SKU组件
let skuForm = ref<any>()
// SKU列表
let skuArr = ref<skuData[]>([])
// SKU列表是否显示
let show = ref<boolean>(false)
//#endregion

// 监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    if (!categoryStore.c3Id) return;
    getHasSpu();
  }
);

// 获取某一个三级分类下全部已有SPU
const getHasSpu = async (pager = 1) => {
  pageNo.value = pager;
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id
  );
  if (result.code == 200) {
    records.value = result.data.records;
    total.value = result.data.total;
  }
};

// 添加新的SPU按钮回调
const addSpu = () => {
  spuForm.value.initAddSpu(categoryStore.c3Id)
  scene.value = 1
}

// 改变场景(暴露给子组件)
const changeScene = (obj: any) => {
  scene.value = obj.flag
  if (obj.params == 'update') {
    getHasSpu(pageNo.value)
  } else {
    getHasSpu()
  }
}

// 修改已有spu按钮回调
const updateSpu = (row: SpuData) => {
  spuForm.value.initHasSpuData(row)
  scene.value = 1
}

// 添加SKU列表回调
const addSku = (row: SpuData) => {
  skuForm.value.initSkuDate(categoryStore.c1Id, categoryStore.c2Id, row)
  scene.value = 2
}

// 查看SKU列表数据
const findSku = async (row: SpuData) => {
  let result: skuInfoData = await reqSkuList(row.id)
  if (result.code == 200) {
    skuArr.value = result.data
    show.value = true
  }
}

// 删除SPU
const deleteSpu = async (row: SpuData) => {
  let result: any = await reqRemoveSpu(row.id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败'
    })
  }
}

onBeforeUnmount(() => {
  categoryStore.$reset();
});
</script>

<style scoped></style>